//package com.angel.bo.auth.client.config.security;
//
//import com.fasterxml.jackson.databind.ObjectMapper;
//import lombok.Data;
//import org.springframework.http.MediaType;
//import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
//import org.springframework.security.core.Authentication;
//import org.springframework.security.core.AuthenticationException;
//import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
//
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.IOException;
//import java.io.InputStream;
//
///**
// * @author zhangwei
// * @date 2018/4/27 13:20
// * @desc 自定义登录接收方式:json.用来代替默认的表单提交接收方式
// */
//public class JsonUsernamePasswordAuthenticationFilter extends UsernamePasswordAuthenticationFilter {
//
//    @Override
//    public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException {
//        //Content-Type 为 application/json
//        if (request.getContentType().equals(MediaType.APPLICATION_JSON_UTF8_VALUE) || request.getContentType().equals(MediaType.APPLICATION_JSON_VALUE)) {
//            ObjectMapper mapper = new ObjectMapper();
//            UsernamePasswordAuthenticationToken authRequest = null;
//            try (InputStream is = request.getInputStream()) {
//                AuthenticationBean authenticationBean = mapper.readValue(is, AuthenticationBean.class);
//                authRequest = new UsernamePasswordAuthenticationToken(authenticationBean.getAccount(), authenticationBean.getPassword());
//            } catch (IOException e) {
//                e.printStackTrace();
//                authRequest = new UsernamePasswordAuthenticationToken("", "");
//            } finally {
//                setDetails(request, authRequest);
//            }
//            return this.getAuthenticationManager().authenticate(authRequest);
//        }
//        //常规表单提交
//        else {
//            return super.attemptAuthentication(request, response);
//        }
//    }
//
//    @Data
//    private static class AuthenticationBean {
//        private String account;
//        private String password;
//        private String lang;
//    }
//}
